
#include "ednn.h"
#include "ednn_port.h"



ednn_size_t ednn_tensor_size(struct ednn_tensor *tensor)
{
    ednn_size_t size;

    if (tensor == EDNN_NULL)
        return 0;

    size = 1;
    for (int i=0; i<tensor->ndim; i++) {
        size *= tensor->dim[i];
    }

    return size;
}

int ednn_tensor_shape_compare(struct ednn_tensor *t1, struct ednn_tensor *t2)
{
    if (t1->ndim != t2->ndim) {
        return -1;
    }

    for (int i=0; i<t1->ndim; i++) {
        if (t1->dim[i] != t2->dim[i]) {
            return i+1;
        }
    }

    return 0;
}

struct ednn_tensor *ednn_tensor_create(ednn_shape_t *dim, ednn_uint32_t ndim)
{
    struct ednn_tensor *tensor = ednn_mem_zalloc(
        ednn_align(sizeof(struct ednn_tensor), EDNN_ALIGN) + \
        ednn_align(ndim*sizeof(ednn_shape_t), EDNN_ALIGN));
    ednn_assert(tensor != EDNN_NULL);

    tensor->dim = (ednn_shape_t *)((ednn_uint8_t *)tensor + \
        sizeof(struct ednn_tensor));
    memcpy(tensor->dim, dim, ndim*sizeof(ednn_shape_t));
    tensor->ndim = ndim;

    return tensor;
}